<template>
	<div class="dict-card">
		<!--查询表单-->
		<el-card class="box-card mb10" shadow="never">
			<template #header>
				<span class="card-title">字典数据值</span>
			</template>
			<el-form ref="searchFormRef" class="search-form" :inline="true" label-position="left" label-width="80px" :model="searchForm">
				<el-row :gutter="30" justify="start">
					<el-col :span="8">
						<el-form-item class="form-item" label="字典标签:" prop="dictLabel">
							<el-input v-model="searchForm.dictLabel" placeholder="请输入字典标签" />
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item class="form-item" label="状态:" prop="status">
							<el-select v-model="searchForm.status" class="el-form-item" clearable placeholder="请选择状态">
								<el-option label="正常" value="0" />
								<el-option label="停用" value="1" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<div class="search-btn">
							<el-button type="primary" @click="handleSearch">查询</el-button>
							<el-button @click="handleReset">重置</el-button>
						</div>
					</el-col>
				</el-row>
			</el-form>
		</el-card>
		<!--查询表格-->
		<el-card class="box-card" shadow="never" style="height: 721px">
			<static-table :tableData="tableData" rowId="dictCode" :loading="loading">
				<template #vux-search>
					<el-row :gutter="12" class="mb10">
						<el-button class="ml8" v-has="'system:dictValue:add'" :disabled="!searchForm.dictType" type="primary" @click="handleAddExtends">
							<el-icon><ele-Plus /></el-icon>
							&nbsp;新增
						</el-button>
					</el-row>
				</template>
				<template #vux-column>
					<vxe-column field="selection" fixed="left" type="checkbox" width="60" />
					<vxe-column field="dictLabel" min-width="150" show-overflow="tooltip" title="字典标签" />
					<vxe-column field="dictValue" min-width="120" show-overflow="tooltip" title="字典键值" />
					<vxe-column field="status" min-width="120" title="状态">
						<template #default="{ row }">
							<span style="margin-right: 10px">{{ row.status == '0' ? '正常' : '停用' }}</span>
							<el-switch active-color="#00B42A" inactive-color="#F04141" v-has="'system:dictValue:update'" :model-value="row.status == '0' ? true : false" @change="handleSwitch(row)" />
						</template>
					</vxe-column>
					<vxe-column field="operation" fixed="right" min-width="120" title="操作">
						<template #default="{ row }">
							<div class="vxe-action">
								<a @click="handleUpdate(row)" v-has="'system:dictValue:update'">修改</a>
								<a @click="handleDelete(row)" v-has="'system:dictValue:delete'">删除</a>
							</div>
						</template>
					</vxe-column>
				</template>
			</static-table>
			<el-pagination background :current-page="pageNum" hide-on-single-page :layout="layout" :page-size="pageSize" :total="dataCount" @current-change="handleCurrentChange" @size-change="handleSizeChange" />
		</el-card>
		<EditDictValueModal ref="editModalRef" @peak-modal-ok="handleModalOk" />
	</div>
</template>
<script name="DictValueComp" setup>
import global from '@/utils/global'
import useTableList from '@/hooks/useTableList'
import useModal from '@/hooks/useModal'
const EditDictValueModal = defineAsyncComponent(() => import('./EditDictValueModal.vue'))
/*
 *@Description: 状态初始化
 *@MethodAuthor: liuYuFeng
 *@Date: 2023-08-28 10:53:36
 */
const state = reactive({
	searchForm: {
		dictType: '',
		dictLabel: '',
	},
	selectionAry: [],
	searchFormRef: null,
})
const { searchForm, searchFormRef } = toRefs(state)

const queryUrl = '/meta-system/web/v1/sysDictData/list'
const { loading, dataCount, handleInitQuery, handleModalOk, tableData, pageNum, pageSize, layout, handleSizeChange, handleCurrentChange } = useTableList(queryUrl, state.searchForm, true, false)

const { editModalRef, handleUpdate } = useModal()
/*
 *@Description: 方法模块
 *@MethodAuthor: liuYuFeng
 *@Date: 2023-08-28 10:48:25
 */
const refRefresh = dictType => {
	state.searchForm.dictType = dictType ? dictType : ''
	handleInitQuery()
}
const handleAddExtends = () => {
	handleUpdate({
		dictType: state.searchForm.dictType,
		flag: 'add',
	})
}
const handleSearch = () => {
	handleInitQuery()
}
const handleReset = () => {
	state.searchFormRef.resetFields()
	handleInitQuery()
}
const handleDelete = row => {
	const ids = []
	ids.push(row.dictCode)
	global.$baseConfirm(
		row ? `是否确认删除名称为"--【${row.dictLabel}】--"的数据?` : '是否确认删除当前选中的数据？',
		'提示',
		() => {
			global.$fetchPostData(`/meta-system/web/v1/sysDictData/remove`, { removeIdList: ids }).then(data => {
				if (data.code == 200) {
					global.$baseMessage(data.msg, 'success')
					handleModalOk(2)
				} else {
					global.$baseMessage(data.msg, 'error')
				}
			})
		},
		() => {}
	)
}
//switch 切换
const handleSwitch = row => {
	global.$baseConfirm(`是否确认更改名称为"--【${row.dictLabel}】--"的用户?`, '提示', () => {
		const user = row
		user.status = row.status == 0 ? 1 : 0
		global.$fetchPostData(`/meta-system/web/v1/sysDictData/editDictData`, user).then(data => {
			if (data.code == 200) {
				global.$baseMessage(data.msg, 'success')
				handleModalOk(2)
			} else {
				global.$baseMessage(data.msg, 'error')
			}
		})
	})
}
defineExpose({
	refRefresh,
})
</script>
<style lang="scss" scoped>
.card-title {
	font-size: 16px;
}
.box-card {
	:deep() {
		.el-card__header {
			padding-left: 14px !important;
		}
	}
}
</style>
